<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
/**
 * 上传文件
 * Class UploadController
 * @package App\Http\Controllers\Common
 */
class UploadController extends Controller
{
    //上传图片
    public function upload(Request $request){
        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $disk = Storage::disk('public');
            $photo = $request->file('image');
            $realPath = $photo->getRealPath();//临时文件路径
            $name = hash_file('md5',$realPath);
            $time = date('Y-m-d');
            $fileName = $time.'/'.$name.'.'.$photo->getClientOriginalExtension();
            $bool = $disk->put($fileName, file_get_contents($realPath));
            // 判断是否上传成功
            if ($bool) {
                $return = ['path'=>$disk->url($fileName)];
                return $this->writeJson(0,$return);
            }
        }
        return $this->writeJson(1,null,'上传错误');
    }

    //上传文件
    public function file(Request $request){
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $disk = Storage::disk('file');
            $photo = $request->file('file');
            $realPath = $photo->getRealPath();//临时文件路径
            $name = $photo->getClientOriginalName();
            $time = date('Y-m-d');
            $fileName = $time.'/'.$name;
            $bool = $disk->put($fileName, file_get_contents($realPath));
            // 判断是否上传成功
            if ($bool) {
                $return = ['path'=>$disk->url($fileName)];
                return $this->writeJson(0,$return);
            }
        }
        return $this->writeJson(1,null,'上传错误');
    }

}
